package com.tl.sun;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.support.multidex.MultiDexApplication;

import com.blankj.utilcode.utils.Utils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cookie.store.PersistentCookieStore;
import com.orhanobut.logger.Logger;
import com.proxy.sosdk.SoProxy;
import com.tl.sun.manager.MDAccountMgr;
import com.tl.sun.manager.URLManager;
import com.tl.sun.model.base.BaseEntity;
import com.tl.sun.service.MyPushIntentService;
import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.zhima.sdk.ZMMgr;

import java.util.logging.Level;


public class AppContext extends MultiDexApplication {

    private static AppContext instance;

    public static String packname;
    public static String version;

    public static int timenubmer = 0;
    public static long remaintime;
    private String mChannelName;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        instance = this;
        packname = getPackageInfo().packageName;
        version = getVersion();

        SoProxy.getInstance().init(this);
        OkGoInit();
        ZMMgr.init(this, getPackageName());

        Utils.init(instance);
        MobclickAgent.setDebugMode(true);
        mChannelName = AnalyticsConfig.getChannel(this);
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.setDebugMode(false);
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                MDAccountMgr.getInstance().setDeviceToken(deviceToken);
                MDAccountMgr.getInstance().writeToCache(instance);
            }

            @Override
            public void onFailure(String s, String s1) {
                Logger.e(s + "  " + s1);
            }
        });

        mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);


    }

    public void OkGoInit() {
        //必须调用初始化
        OkGo.init(this);
        try {
            OkGo.getInstance()
                    .debug("ZMDL", Level.INFO, true)
                    //如果使用默认的 60秒,以下三行也不需要传
                    .setConnectTimeout(30000)  //全局的连接超时时间
                    .setReadTimeOut(30000)     //全局的读取超时时间
                    .setWriteTimeOut(30000)    //全局的写入超时时间

                    //可以全局统一设置缓存模式,默认是不使用缓存,可以不传,具体其他模式看 github 介绍 https://github.com/jeasonlzy0216/
//                    .setCacheMode(CacheMode.NO_CACHE)

                    //可以全局统一设置缓存时间,默认永不过期,具体使用方法看 github 介绍
//                    .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                    .setCookieStore(new PersistentCookieStore());        //cookie持久化存储，如果cookie不过期，则一直有效
            //设置全局公共参数
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得当前app运行的AppContext
     *
     * @return
     */
    public static AppContext getInstance() {
        return instance;
    }


    /**
     * 获取App安装包信息
     *
     * @return
     */
    public PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace(System.err);
        }
        if (info == null)
            info = new PackageInfo();
        return info;
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "1.0.0";
        }
    }

    public String getBaseUrl() {
        return URLManager.getBaseUrlRandom();
//        return "http://test.zmdl.321174.com/api/";
    }

    public String getPayUrl() {
        return URLManager.PAY_URL;
//        return "http://test.zmdl.321174.com/";
    }

    public static BaseEntity getBaseEntity(String json) {
        Gson gson = new Gson();
        BaseEntity base = gson.fromJson(json, BaseEntity.class);
        return base;
    }

    public String getChannelName() {
        return mChannelName;
    }

    public void setChannelName(String channelName) {
        mChannelName = channelName;
    }
}
